package com.qiaotongtianxia.loans.adapters;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qiaotongtianxia.loans.R;
import com.qiaotongtianxia.loans.bean.Refund;
import com.qiaotongtianxia.loans.functionInterface.IClickListener;
import com.qiaotongtianxia.loans.util.CommonUtils;
import com.qiaotongtianxia.loans.views.BaseTextView;
import com.qiaotongtianxia.loans.views.RoundCornerTextView;
import com.qiaotongtianxia.loans.views.refrushRecyclerView.BaseViewHolder;
import com.qiaotongtianxia.loans.views.refrushRecyclerView.RecyclerAdapter;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lisen on 2017/12/15.
 *
 * @author lisen < 4533548588@qq.com >
 */

public class WaitRefundAdapter extends RecyclerAdapter<Refund> {

    private Context context;

    public WaitRefundAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public BaseViewHolder<Refund> onCreateBaseViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_wait_refund, parent, false);
        return new Holder(view);
    }

    @SuppressWarnings("all")
    class Holder extends BaseViewHolder<Refund> {

        @BindView(R.id.tv_amount)
        BaseTextView tvAmount;
        @BindView(R.id.tv_time)
        BaseTextView tvTime;
        @BindView(R.id.tv_deadLine)
        BaseTextView tvDeadLine;
        @BindView(R.id.tv_upload)
        RoundCornerTextView tvUpload;
        @BindView(R.id.tv_status)
        RoundCornerTextView tvStatus;
        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }

        @Override
        public void setData(final Refund data) {

            tvAmount.setText(data.getAmount()+"元");

            tvStatus.setVisibility(View.GONE);
            if (!TextUtils.isEmpty(data.getRefund_time())){
                tvDeadLine.setText("到期日期:" + data.getRefund_time().split(" ")[0]);

                long refundTime = CommonUtils.parseStringToTime(data.getRefund_time(), 0);
                long time = CommonUtils.parseStringToTime(data.getUpdate_time(),0);
                int day = CommonUtils.getDayCounts((refundTime - time)*1d/1000L);
                if (day < 0){
                    day = 0;
                }
                tvTime.setText(day+"天");

                long checkTime =  CommonUtils.parseStringToTime(data.getCheck_time(),0);
                if (checkTime == 0){
                    checkTime = CommonUtils.getTodayStartTime();
                }

                long dTime = checkTime - refundTime;
                if ( dTime > 0){
                    tvStatus.setVisibility(View.VISIBLE);
                    tvStatus.setText("逾期");
                    tvStatus.setCornerColor(ContextCompat.getColor(context,R.color.red));
                    tvTime.setVisibility(View.GONE);
//                    tvTime.setText(CommonUtils.getDayCounts(dTime*1d/1000)+"天");
                }else{
                    tvTime.setVisibility(View.VISIBLE);
                    tvTime.setText(day+"天");
                }
            }else{
                tvTime.setVisibility(View.VISIBLE);
                tvDeadLine.setText("暂无");
                tvTime.setText("暂无");
            }

            tvUpload.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (iClickListener != null){
                        iClickListener.onClick(data,getLayoutPosition());
                    }
                }
            });
        }
    }

    private IClickListener<Refund> iClickListener;

    public void setiClickListener(IClickListener<Refund> iClickListener) {
        this.iClickListener = iClickListener;
    }
}
